package com.imy.luascript.app.ui.permission;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.RelativeLayout;

import com.imy.common.util.ALog;
import com.imy.common.util.SPUtil;
import com.imy.common.util.ToastUtil;
import com.imy.luascript.R;
import com.imy.luascript.base.activity.BaseViewActivity;
import com.imy.luascript.constant.SPConstant;
import com.imy.luascript.viewmodel.BaseViewModel;
import com.suke.widget.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;

public class PermissionSettingActivity extends BaseViewActivity {


    @BindView(R.id.battery_sb)
    SwitchButton batterySb;
    @BindView(R.id.battery_rl)
    RelativeLayout batteryRl;
    @BindView(R.id.wake_lock_sb)
    SwitchButton wakeLockSb;
    @BindView(R.id.wake_lock_rl)
    RelativeLayout wakeLockRl;

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void initView() {
        setToolbarTitle("偏好设置");
        initWakeLock();
    }

    private void initWakeLock() {
        boolean aBoolean = SPUtil.getBoolean(SPConstant.WAKE_LOCK_OPEN, true);
        wakeLockSb.setChecked(aBoolean);
        wakeLockSb.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                SPUtil.setBoolean(SPConstant.WAKE_LOCK_OPEN,isChecked);
                ToastUtil.toast(self,"下次启动后生效");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initBattery();
    }

    private void initBattery() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            boolean ignoringBatteryOptimizations = powerManager.isIgnoringBatteryOptimizations(getPackageName());
            ALog.i("ignoringBatteryOptimizations:" + ignoringBatteryOptimizations);
            batterySb.setChecked(ignoringBatteryOptimizations);
            batterySb.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                    if (!isChecked) {
                        Intent intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
                        ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, 0);
                        if (resolveInfo != null) {
                            startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivityForResult(intent, 1000);
                    }
                    batterySb.setOnCheckedChangeListener(null);
                }
            });
        } else {
            batteryRl.setVisibility(View.GONE);
        }

    }


    @Override
    protected int getLayoutID() {
        return R.layout.activity_permission_setting;
    }


}
